/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AppServices;

import Facades.UsersFacade;
import Facades.UsersFacadeRemote;
import VOs.Plan;
import VOs.Users;
import java.util.Collection;
import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

/**
 *
 * @author Julian
 */
public class UsersManage {

    static public UsersFacadeRemote StartContext() throws NamingException{
        InitialContext ctx = new InitialContext();
        UsersFacadeRemote ubeanremote = (UsersFacadeRemote) ctx.lookup("Facades.UsersFacadeRemote");
        return ubeanremote;
    }

    static public boolean checklogin(String username, String pass) throws Exception {
        boolean result = false;
        Users u = new Users();

        try {
            //InitialContext ctx1 = new InitialContext();
            //UsersFacadeRemote ubean = (UsersFacadeRemote) ctx1.lookup("Facades.UsersFacadeRemote");


            u = StartContext().findid(username);
            if (u.getPassword().equals(pass)) {
                result = true;
            }//*/
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    static public Users User(String username) throws Exception {
       Users u = new Users();
        try {
            u = StartContext().findid(username);
        } catch (Exception ex) {
            throw ex;
        }
        return u;
    }


    static public boolean createUsers(String username, String name, String lastname, String phone, String pass,String creditcard,String email,String identification) throws Exception {
        boolean result = false;

        try {
            Users u = new Users(username, name, lastname, phone, pass, creditcard, email);
            u.setIdentification(identification);
            
            StartContext().create(u);
            
            result = true;
            
        } catch (Exception ex) {
            result = false;
            throw ex;
        }

        return result;
    }

    static public boolean editUsers(String username, String name, String lastname, String phone, String pass,String creditcard,String email) throws Exception {
        boolean result = false;

        Users ubdnew = new Users();


        try {
            //InitialContext ctx1 = new InitialContext();
            //InitialContext ctx1 = (InitialContext) a;
            //UsersFacadeRemote ubean = (UsersFacadeRemote) ctx1.lookup("Facades.UsersFacadeRemote");

            ubdnew = StartContext().findid(username);
            ubdnew.setName(name);
            ubdnew.setLastname(lastname);
            ubdnew.setPassword(pass);
            ubdnew.setCreditcard(creditcard);
            ubdnew.setEmail(email);
            ubdnew.setPhone(phone);

            StartContext().edit(ubdnew);
            result = true;

        } catch (Exception ex) {
            result = false;
            throw ex;
        }

        return result;
    }

    public static boolean checkAdminLogin(String username, String pass){
        boolean result = false;
        Hashtable authEnv = new Hashtable(11);
    	//String userName = "hayleyparamore";
    	//String passWord = "crushcrushcrush";
    	String base = "ou=people,dc=turismo,dc=un,dc=org";
    	String dn = "cn=" + username + "," + base;
    	String ldapURL = "ldap://192.168.0.100:389";

    	authEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
   		authEnv.put(Context.PROVIDER_URL, ldapURL);
   		authEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
   		authEnv.put(Context.SECURITY_PRINCIPAL, dn);
   		authEnv.put(Context.SECURITY_CREDENTIALS, pass);

    	try {
    		DirContext authContext = new InitialDirContext(authEnv);
    		System.out.println("Authentication Success!");
                result = true;
    	} catch (AuthenticationException authEx) {
                authEx.printStackTrace();
    		System.out.println("Authentication failed!");
                result = false;

    	} catch (NamingException namEx) {
    		System.out.println("Something went wrong!");
    		namEx.printStackTrace();
    	}
        return result;
    }

}
